// <copyright file="BaseDataTableHandler.cs" company="YUIAsp.Net">
// Copyright (c) 2009 All Right Reserved
// </copyright>
// <author>raul popescu</author>
// <email>dev@yuiasp.net</email>
// <date>2009-01-30</date>
// <summary>YUIAsp.Net File</summary>

using System.Web;
using System.Web.SessionState;

namespace YUIAspNet 
{
    /// <summary>
    /// Base handler to feed DataTable items.
    /// </summary>
    public abstract class BaseDataTableHandler : IHttpHandler, IRequiresSessionState
    {
        //request is like: http://localhost:4396/DummyDataTableHandler.asmx?sort=id&dir=asc&startIndex=50&results=25
        #region IHttpHandler Members

        /// <summary>
        /// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler"/> instance.
        /// </summary>
        /// <value></value>
        /// <returns>true if the <see cref="T:System.Web.IHttpHandler"/> instance is reusable; otherwise, false.</returns>
        public bool IsReusable
        {
            get { return false; }
        }

        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
        public void ProcessRequest(HttpContext context)
        {
            //int virtualItemsCount = GetVirtualItemsCount();
            DataSourceBase src = PopulateData();

            context.Response.Write(src.Serialize());

            //context.Response.Write(string.Format("{{'recordsReturned':{0},'totalRecords':{1},'startIndex':{2},'sort':'{3}','dir':'{4}','pageSize':{5},'records': ", src.Rows.Count, virtualItemsCount,this.StartIndex, this.SortColumnKey, this.SortDirection, this.ResultsPerPage));
            //context.Response.Write(DataTable.TransformSource(src).ToString());
            //context.Response.Write("}");
        }
        #endregion

        /// <summary>
        /// Gets the data.
        /// </summary>
        /// <returns></returns>
        public abstract DataSourceBase PopulateData();

        /// <summary>
        /// Gets the start index.
        /// </summary>
        /// <value>The start index.</value>
        public static int StartIndex 
        {
            get 
            {
                int startIndex;
                if (int.TryParse(HttpContext.Current.Request.QueryString["startIndex"], out startIndex))
                    return startIndex;
                return 0; //startIndex was either not present, or not an integer...
            }
        }

        /// <summary>
        /// Gets the results per page.
        /// </summary>
        /// <value>The results per page.</value>
        public static int ResultsPerPage
        {
            get
            {
                int resultsPerPage;
                if (int.TryParse(HttpContext.Current.Request.QueryString["results"], out resultsPerPage))
                    return resultsPerPage;
                return 0; //resultsPerPage was either not present, or not an integer...
            }
        }

        /// <summary>
        /// Gets the sort column key.
        /// </summary>
        /// <value>The sort column key.</value>
        public static string SortColumnKey
        {
            get 
            {
                return HttpContext.Current.Request.QueryString["sort"];
            }
        }

        /// <summary>
        /// Gets the sort direction.
        /// </summary>
        /// <value>The sort direction.</value>
        public static string SortDirection
        {
            get
            {
                return HttpContext.Current.Request.QueryString["dir"];
            }
        }
    }
}
